//
//  MovieMessageViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MovieMessageViewController.h"
#import "UIImageView+WebCache.h"
#import "movieModel.h"
#import "DataBaseHandle.h"
#import "MyModel.h"
#import "UserModel.h"
@interface MovieMessageViewController ()


@end

@implementation MovieMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = self.model.title;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shoucang.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    [self createUI];
}


- (void)createUI {
    UIImageView *imagView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 150, 210)];
    imagView.backgroundColor = [UIColor yellowColor];
    [imagView sd_setImageWithURL:[NSURL URLWithString:self.model.imag]];
    [self.view addSubview:imagView];
    [imagView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 150, 50)];
//    titleLabel.backgroundColor = [UIColor cyanColor];
    titleLabel.text = self.model.title;
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    
    UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 100, 160, 40)];
//    ratingLabel.backgroundColor = [UIColor cyanColor];
    NSString *str = [NSString stringWithFormat:@"评分：%@", self.model.rating];
    ratingLabel.text = str;
    [self.view addSubview:ratingLabel];
    [ratingLabel release];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 150, 160, 40)];
//    timeLabel.backgroundColor = [UIColor cyanColor];
    timeLabel.text = self.model.pubdate;
    [self.view addSubview:timeLabel];
    [timeLabel release];
    
    UILabel *starsLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 160, 40)];
//    starsLabel.backgroundColor = [UIColor cyanColor];
    NSString *str1 = [NSString stringWithFormat:@"⭐️%@", self.model.stars];
    starsLabel.text = str1;
    [self.view addSubview:starsLabel];
    [starsLabel release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 电影收藏
- (void)itemAction:(UIBarButtonItem *)item {
    
    [[DataBaseHandle sharedDataBaseHandle] createTable];
    // 先去数据库判断是否有用户登录
    UserModel *user = [[DataBaseHandle sharedDataBaseHandle] selectUserState:@"是"];
    if (user.userName == nil) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户未登录" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];

    } else {
        // 如果有用户登录
        MyModel *model = [MyModel myModel];
        model.title = self.model.title;
        model.userName = user.userName;
        MyModel *temp = [[[DataBaseHandle sharedDataBaseHandle] selectModel:model table:@"Movie"] lastObject];
        if (temp == nil) {
            [[DataBaseHandle sharedDataBaseHandle] insertModel:model table:@"Movie"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            [[DataBaseHandle sharedDataBaseHandle] deleteModel:model table:@"Movie"];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
        }

    }
   
  
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
